// order detail
// ============

define(['zepto', 'utils', 'component/countdown'], function($, utils){
  return {
    init: function() {
    	this.cancelOrder();
    	this.cancelBooking();
    },
    
    cancelOrder: function(orderId, agentMobile) {
        var currentThat = this;
    	$(".cancelOrder").on('click', function(e){
    		var that = this
    		$.confirm('确认取消订单?', function () {
    			var defer = $.Deferred()
                $.showIndicator()
                $.post($(that).data('href'), {"_method" : "PATCH"})
                  .done(function(response) {
                    $.hideIndicator()
                    utils.processResult(response, function(result) {
                        if (result.message.length > 10) {
                          $.toast(result.message, 2000, "large-toast")
                        }else {
                          $.toast(result.message || '取消订单成功')
                        }
                    	currentThat.requestOrderCancelMessage($(that).data("agent"), $(that).data("order"), function(data){},function(){});
                    	setTimeout(function() {
                            window.location.href = window.__CTX__.root + "/orders"; 
                       }, 2000)
                    }, function(result) {
                      $.toast(result.message || '取消订单失败')
                    })
                  })
                return defer.promise();
    	    });

    	})
    	
    },
    cancelBooking: function(orderId, agentMobile) {
        var currentThat = this;
    	$(".cancelBooking").on('click', function(e){
    		var that = this
    		$.confirm('确认取消预定?', function () {
    			var defer = $.Deferred()
                $.showIndicator()
                $.post($(that).data('href'), {"_method" : "PATCH"})
                  .done(function(response) {
                    $.hideIndicator()
                    utils.processResult(response, function(result) {
                    	$.toast(result.message || '取消预定成功')
                    	currentThat.requestOrderCancelMessage($(that).data("agent"), $(that).data("order"), function(data){},function(){});
                    	setTimeout(function() {
                            window.location.href = window.__CTX__.root + "/orders"; 
                       }, 2000)
                    }, function(result) {
                      $.toast(result.message || '取消预定失败')
                    })
                  })
                return defer.promise();
    		});
    		
    	})
    },
    
    /**
     * Request server to send order cancel message to mobile
     */
    requestOrderCancelMessage: function(mobile, orderId, success, failure) {
      $.showIndicator()
      utils.requestCancelOrderMessage(mobile, orderId, function(result) {
        $.hideIndicator()
        success && success(result.data)
      }, failure)
    }
    
  }
})